<?php

namespace app\shop\controller\employ;

use app\common\model\store\Store;
use app\shop\controller\Controller;
use app\shop\model\employ\Poster as PosterModel;

class Poster extends Controller
{
    /**
     * 福利官列表
     */
    public function index()
    {
        // 供应商列表
        $model = new PosterModel;
        $list = $model->getList($this->postData());

        $store_list = Store::where('is_delete',0) -> field('store_name,shop_supplier_id') -> select();
        $store_array = [];
        foreach ($store_list as $v){
            $store_array[$v['shop_supplier_id']][] = $v['store_name'];
        }
        foreach ($list as $key => $value){
            $list[$key]['store_name'] = $store_array[$value['shop_supplier_id']]??'';
        }

        return $this->renderSuccess('', compact('list'));
    }

    public function audit($poster_id, $audit, $reject_reason="")
    {
        $model = PosterModel::detail($poster_id);
        if ($model["is_audit"] != 0) {
            return $this->renderError('请勿重复操作');
        }
        if (!in_array($audit, [1, 2])) {
            return $this->renderError('非法操作');
        }
        if ($audit == 2 && empty(trim($reject_reason))) {
            return $this->renderError('请填写不通过原因');
        }
        $model->save(['is_audit' => $audit, 'reject_reason' => $audit == 2 ? trim($reject_reason) : $model['reject_reason']]);
        return $this->renderSuccess('', '操作成功');
    }

    public function delete($poster_id)
    {
        $model = new PosterModel;
        $model->where("poster_id",$poster_id)->save(["is_delete"=>1]);
        return $this->renderSuccess('操作成功');
    }
}